<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>{$lang.title}</title>
    <include file="common/tag"/>
</head>
<body>
<div style=" margin: 30px;">

    <include file="common/search"/>

    <table class="layui-hide" id="datatable" lay-filter="datatable" lay-size="sm" lay-skin="row"></table>

</div>
</body>
</html>
<script type="text/html" id="operate">
    <a class="layui-btn layui-btn-xs" lay-event="edit" style="display: <?php if (!in_array(str_replace('/','',$__url).'--edit',think\Session::get('button-resource')) && think\Session::get('username')!='admin') { ?>  none <?php } ?>"">{$lang.edit}</a>
    <a class="layui-btn layui-btn-danger layui-btn-xs" lay-event="del" style="display: <?php if (!in_array(str_replace('/','',$__url).'--del',think\Session::get('button-resource')) && think\Session::get('username')!='admin') { ?>  none <?php } ?>"">{$lang.del}</a>
</script>
<include file="common/js"/>
<script>

    $(function(){
        layui.use(['table','laydate'], function(){
            let table = layui.table;
            let laydate = layui.laydate;

            //渲染range时间控件
            $.each($('.search_date_range'),function (k) {
                let id = $(this).attr('id');
                laydate.render({
                    elem: '#'+id
                    ,format: 'yyyy-MM-dd'
                    ,value: $("#"+id).val()
                    ,range: true
                    ,trigger:'click'
                });
            });
            //渲染单天时间控件
            $.each($('.search_date'),function (k) {
                let id = $(this).attr('id');
                laydate.render({
                    elem: '#'+id
                    ,value: $("#"+id).val()
                    ,range: false
                    ,trigger:'click'
                });
            });

            table.render({
                elem: '#datatable'
                ,url:'{$__url}'
                ,method: 'POST'
                ,toolbar: '#toolbar'
                ,cellMinWidth: 80
                ,even: true
                ,size: 'lg'
                ,id: 'datatable'
                ,where: postSearchData()
                ,parseData: function(resp){ //res 即为原始返回的数据
                    if(resp.code==302){
                        layer.msg("{$lang.token_exceed}");
                        parent.location.reload();
                        return;
                    }
                    let tableRows = resp.data;
                    tableRows.forEach((elem, index) => {
                        elem['status'] = (elem['status']==1)?'<span class="layui-badge-dot layui-bg-green"></span>':'<span class="layui-badge-dot layui-bg-cyan"></span>';
                    });
                    return {
                        "code": resp.code,
                        "msg": resp.msg,
                        "count": resp.count,
                        "data": tableRows,
                    };
                }
                ,cols: [{$__header}]
                ,page: true
            });

            table.on('tool(datatable)', function(obj){
                let data = obj.data;
                let id = data.id;
                if(obj.event === 'del'){
                    let ids = [];
                    //获取复选框的值
                    let checkStatus = layui.table.checkStatus('datatable').data;
                    if (checkStatus.length <= 0)
                        ids = [id];
                    for (const item of checkStatus){
                        ids.push(item['id']);
                    }
                    layer.confirm('{$lang.confirm_del}', function(index){
                        $.post("/mottoDel",
                            {
                                ids:ids,
                            },
                            function(data,status){
                                if (data['code']==1){
                                    layer.open({
                                        title: '{$lang.notice}',
                                        content: data['msg']
                                        ,yes: function(index1, layero){
                                            obj.del();
                                            layer.close(index1);
                                        },cancel: function(){
                                        }
                                    });
                                }else{
                                    layer.msg(data['msg']);
                                }
                            }, "json"
                        );
                        layer.close(index);
                        $('.data-search .layui-btn').click();
                    });
                }
                else if(obj.event === 'edit'){
                   layer.open({
                        type: 2,
                        area: ['750px','480px'],
                        title: '{$lang.edit}',
                        content: "/mottoForm?method=edit&id="+id
                    });
                }
            });

            let active = {
                reload: function(){
                    //执行重载
                    table.reload('datatable', {
                        page: {
                            curr: $(".layui-laypage-em").next().html()
                        }
                        ,where: postSearchData()
                    }, 'data');
                }
            };

            $('.data-search .layui-btn').on('click', function(){
                let type = $(this).data('type');
                active[type] ? active[type].call(this) : '';
            });

        });

        $('.data-add .layui-btn').on('click', function(){
            layer.open({
                type: 2,
                area: ['750px','480px'],
                title: '{$lang.add}',
                content: "/mottoForm?method=add"
            });
        });

    });


</script>